package com.test.practice.io;

import lombok.Cleanup;

import java.io.*;

/**
 * @ClassName Demo
 * ==================
 * @Description Demo
 * ==================
 * @Author jovi
 * ==================
 * @Date 2020-04-18 23:14
 * ==================
 * @Version 1.0
 */
public class Demo {

    public static void main(String[] args) throws IOException {
        //实例化File对象
        File file = new File("ttt1.txt");
        //判断当前文件是否存在，以及是否是文件
        if (file.exists() && file.isFile()) {
            System.out.println("文件已存在，覆盖内容");
            System.out.println();
        } else {
            System.out.println("文件不存在，新建一个文件");
            System.out.println(file.createNewFile());
        }

        //实例化BufferedReader对象
        @Cleanup BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line;
        //实例化DataOutputStream对象
        @Cleanup BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
        @Cleanup DataOutputStream dos = new DataOutputStream(bos);
        while ((line = br.readLine()) != null) {
            if (line.equalsIgnoreCase("end")) {
                break;
            }
            dos.write(line.getBytes());
            dos.writeBytes("\r\n");
        }

        @Cleanup BufferedReader br_read = new BufferedReader(new FileReader(file));
        System.out.println("\n输出文件内容===============");
        while ((line = br_read.readLine()) != null) {
            System.out.print(line + "\r\n");
        }
        System.out.println("===========文件读取完毕==========");
    }

}
